import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import utils from '@/utils/utils'
import http from '@/api/account'

const userInfoStore = defineStore('userInfo', {
  state: () => {
    return {
      userInfo: JSON.parse(sessionStorage.getItem('userInfo')) || {},
      menuList: JSON.parse(sessionStorage.getItem('menuList')) || [],
      whiteList: JSON.parse(sessionStorage.getItem('whiteList')) || []
    }
  },
  getters: {

  },
  actions: {
    getUserInfo() {
      http.userInfo().then(res => {
        this.userInfo = res.data.userInfo
        sessionStorage.setItem('userInfo', JSON.stringify(this.userInfo))
        this.setMenuList(res.data.menuList)
      })
    },
    setMenuList(menuList) {
      this.menuList = menuList
      this.whiteList = utils.flatten(this.menuList)
      sessionStorage.setItem('whiteList', JSON.stringify(this.whiteList))
      sessionStorage.setItem('menuList', JSON.stringify(this.menuList))
    },
    clearInfo() {
      sessionStorage.removeItem('userInfo')
      sessionStorage.removeItem('menuList')
      sessionStorage.removeItem('whiteList')
    }
  },
})

export default userInfoStore